package com.daocao.hua.impl;

import android.app.Activity;
import android.content.Context;

import com.daocao.hua.constant.Const;
import com.daocao.hua.http.CallBackListener;
import com.daocao.hua.http.InterfaceServer;
import com.daocao.hua.http.InfoCallBack;
import com.daocao.hua.interfaces.ZhiMaInfo;
import com.daocao.hua.model.RequestModel;
import com.daocao.hua.model.ResponseModel;
import com.daocao.hua.model.ZhiMaCertModel;
import com.daocao.hua.model.ZhiMaModel;

import java.util.HashMap;
import java.util.Map;

/**
 * 芝麻信用数据
 * Created by Master.Dong on 2016/4/13.
 */
public class ZhiMaInfoImpl implements ZhiMaInfo {


    @Override
    public void doCreditRequest(Activity activity, String params, String sign, final InfoCallBack callBack) {
////测试数据，此部分数据，请由商户服务端生成下发，具体见开放平台商户对接文档
//        //请注意params、sign为encode过后的数据
////        String params = "pr8pePYsRTx0SjrbUnNkd4cgAS1Pt%2FsDHGl%2BOuu23tKRH41GFPsT097JgTL0BU6I36kigNRdoS0bKCE%2Bii2WwSV0Z9%2BW3K4%2FPNTkQKMdhL4BarNdd67Usuk9mRYjjkcL8Axzh%2F4eQARuEqx2ZTvmbXnXn2vog6pU%2BtHFhB03RAZ4DfyO3yizqBPdDfTJNcq6kW%2FGQpk6pDCXAqf2UNB90yOCb3GsQSWtL4xZj1eFFpBQxUKNF6YVnFEnT60FM7NsiGzRKgcYvHN6FbCuuFoEI6xW6LvEsFI%2BxXp3Q6vb6KQlcyXMbaAtPHBxhMl%2BP0%2BV8EmmAf8bpGo1BsBujjEy740aF8mobrti2STcm1vyFQZxql8rLEB%2Bm%2BnS8GqA%2BoZQM3gFodkCN9rKfiOi6eSPrBNG4d%2BxE8uecc6zGIVBfxbMSpIy8mZSNFwTqxOb08NP9eshpxqqZHC2sZNJBwzsu542HG9xK7JL2m27LVz6PaeVDPKMUwX88z%2BzRsMB1%2Beo";
//        String appId = "1000897";
////        String sign = "XB7b5PsRVJlNeiHATuDkDvfp6tku%2FieZZ06WWWvMrgTJsK6Gxzy2u19c8jPdopCCRCuvHo2w7EqalV3hojhGbjXG%2Ff9hpJ1S9ZMcDxju75nY5IMA52EaNmZpXSgB665TeEb4qlHOhQvgyZaEmKEfo9cL9zd2DTm5M%2B5dEjlLVxY%3D";
//        //extParams参数可以放置一些额外的参数，例如当biz_params参数忘记组织auth_code参数时，可以通过extParams参数带入auth_code。
//        //不过建议auth_code参数组织到biz_params里面进行加密加签。
//        Map<String, String> extParams = new HashMap<>();
//        extParams.put("auth_code", "M_APPSDK");
//
//
//        try {
//            //请求授权
//            CreditAuthHelper.creditAuth(activity, appId, params, sign, extParams, new ICreditListener() {
//                @Override
//                public void onComplete(Bundle result) {
//                    //toast message
////                    demoView.toastMessage("授权成功");
//                    //从result中获取params参数,然后解析params数据,可以获取open_id。
//                    try {
//                        if (result != null) {
//                            ZhiMaModel model = new ZhiMaModel();
//                            ZhiMaModel.Data data = new ZhiMaModel.Data();
//
//                            Set<String> keys = result.keySet();
//                            for (String key : keys) {
//                                Log.d("返回的芝麻---------", "芝麻参数：      " + key + " = " + result.getString(key));
//
//                                if (key.equals("params"))
//                                    data.setParams(result.getString(key));
//                                if (key.equals("sign"))
//                                    data.setSign(result.getString(key));
//
//                            }
//                            model.setData(data);
//                            callBack.onSuccess(model);
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//
//                @Override
//                public void onError(Bundle result) {
//                    //toast message
////                    demoView.toastMessage("授权错误");
//                    Log.d("     ", "DemoPresenterImpl.doCreditAuthRequest.onError.");
//                    callBack.onFailure("授权错误");
//                }
//
//                @Override
//                public void onCancel() {
//                    //toast message
////                    demoView.toastMessage("授权失败");
//                    Log.d("     ", "DemoPresenterImpl.doCreditAuthRequest.onCancel.");
//                    callBack.onFailure("授权失败");
//                }
//            });
//        } catch (Exception e) {
//            Log.e("    ", "DemoPresenterImpl.doCreditAuthRequest.exception=" + e.toString());
//        }
    }

    @Override
    public void getZhiMaAuthInfo(Context mContext, String deal_id, String name, String idcard, final InfoCallBack callBack) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        mapData.put("deal_id", deal_id);
        mapData.put("name", name);
        mapData.put("idcard", idcard);
        RequestModel model = new RequestModel(mapData);

        InterfaceServer.getInstance(mContext.getApplicationContext()).requestInterface(Const.ZHI_MA_AUTH, Const.RequestMethod.POST, model, Const.DEFAULT_TIMEOUT,new CallBackListener<ZhiMaModel>(ZhiMaModel.class,callBack));
//                new ResponseListener() {
//            @Override
//            public void onStartInMainThread(Object result) {
//                callBack.onFailure(result.toString());
//            }
//
//            @Override
//            public void onFinishInMainThread(Object result) {
//            }
//
//            @Override
//            public Object onSuccessInRequestThread(int statusCode, String content) {
//                try {
//                    return JsonUtil.<ZhiMaModel>fromJson(content, ZhiMaModel.class);
//                } catch (Exception e) {
//                    return null;
//                }
//            }
//
//            @Override
//            public void onSuccessInMainThread(int statusCode, String content, Object result) {
//                try {
//                    ZhiMaModel model = (ZhiMaModel) result;
//                    if (model.getStatus() == 1)
//                        callBack.onSuccess(model);
//                    else callBack.onFailure(model.getMsg());
//                    //返回数据接口
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }


    @Override
    public void getZhiMaValidateInfo(Context mContext, String params, String sign, final InfoCallBack callBack) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        mapData.put("params", params);
        mapData.put("sign", sign);
        RequestModel model = new RequestModel(mapData);

        InterfaceServer.getInstance(mContext.getApplicationContext()).requestInterface(Const.ZHI_MA_VALIDATE, Const.RequestMethod.POST, model, Const.DEFAULT_TIMEOUT,new CallBackListener<ResponseModel>(ResponseModel.class,callBack));
//                new ResponseListener() {
//            @Override
//            public void onStartInMainThread(Object result) {
//                callBack.onFailure(result.toString());
//            }
//
//            @Override
//            public void onFinishInMainThread(Object result) {
//            }
//
//            @Override
//            public Object onSuccessInRequestThread(int statusCode, String content) {
//                try {
//                    return JsonUtil.<ResponseModel>fromJson(content, ResponseModel.class);
//                } catch (Exception e) {
//                    return null;
//                }
//            }
//
//            @Override
//            public void onSuccessInMainThread(int statusCode, String content, Object result) {
//                try {
//                    ResponseModel model = (ResponseModel) result;
//                    if (model.getStatus() == 1)
//                        callBack.onSuccess(model.getMsg());
//                    else callBack.onFailure(model.getMsg());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }

    /**
     * 获取biz_no和url
     *
     * @param mContext
     * @param schema
     * @param callBack
     */
    @Override
    public void getZhiMaCertInfo(Context mContext, String schema, final InfoCallBack callBack) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        mapData.put("schema", schema);
        RequestModel model = new RequestModel(mapData);

        InterfaceServer.getInstance(mContext.getApplicationContext()).requestInterface(Const.ZHI_MA_CERT, Const.RequestMethod.POST, model, Const.DEFAULT_TIMEOUT, new CallBackListener<ZhiMaCertModel>(ZhiMaCertModel.class,callBack));
//                new ResponseListener() {
//            @Override
//            public void onStartInMainThread(Object result) {
//                callBack.onFailure(result.toString());
//            }
//
//            @Override
//            public void onFinishInMainThread(Object result) {
//            }
//
//            @Override
//            public Object onSuccessInRequestThread(int statusCode, String content) {
//                try {
//                    return JsonUtil.<ZhiMaCertModel>fromJson(content, ZhiMaCertModel.class);
//                } catch (Exception e) {
//                    return null;
//                }
//            }
//
//            @Override
//            public void onSuccessInMainThread(int statusCode, String content, Object result) {
//                try {
//                    ZhiMaCertModel model = (ZhiMaCertModel) result;
//                    callBack.onSuccess(model);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }

    /**
     * 认证结果查询
     *
     * @param mContext
     * @param biz_no
     * @param callBack
     */
    @Override
    public void getZhiMaCertqueryInfo(Context mContext, String biz_no, final InfoCallBack callBack) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        mapData.put("biz_no", biz_no);
        RequestModel model = new RequestModel(mapData);

        InterfaceServer.getInstance(mContext.getApplicationContext()).requestInterface(Const.ZHI_MA_CERTQUERY, Const.RequestMethod.POST, model, Const.DEFAULT_TIMEOUT,new CallBackListener<ResponseModel>(ResponseModel.class,callBack));
//                new ResponseListener() {
//            @Override
//            public void onStartInMainThread(Object result) {
//                callBack.onFailure(result.toString());
//            }
//
//            @Override
//            public void onFinishInMainThread(Object result) {
//            }
//
//            @Override
//            public Object onSuccessInRequestThread(int statusCode, String content) {
//                try {
//                    return JsonUtil.<ResponseModel>fromJson(content, ResponseModel.class);
//                } catch (Exception e) {
//                    return null;
//                }
//            }
//
//            @Override
//            public void onSuccessInMainThread(int statusCode, String content, Object result) {
//                try {
//                    ResponseModel model = (ResponseModel) result;
//                    if (model.getStatus() == 1)
//                        callBack.onSuccess(model.getMsg());
//                    else callBack.onFailure(model.getMsg());
//                    //返回数据接口
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }
}
